package com.ruoyi.activiti.controller;

import java.util.List;

import com.ruoyi.activiti.service.IProcessService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.activiti.domain.BizExam;
import com.ruoyi.activiti.service.IBizExamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 试卷审核Controller
 * 
 * @author marui
 * @date 2023-10-28
 */
@RestController
@RequestMapping("/exam/exam")
public class BizExamController extends BaseController
{
    @Autowired
    private IBizExamService bizExamService;
    @Autowired
    private IProcessService processService;

    /**
     * 查询试卷审核列表
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizExam bizExam)
    {
        startPage();
        List<BizExam> list = bizExamService.selectBizExamList(bizExam);
        return getDataTable(list);
    }

    /**
     * 导出试卷审核列表
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:export')")
    @Log(title = "试卷审核", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizExam bizExam)
    {
        List<BizExam> list = bizExamService.selectBizExamList(bizExam);
        ExcelUtil<BizExam> util = new ExcelUtil<BizExam>(BizExam.class);
        return util.exportExcel(list, "exam");
    }

    /**
     * 获取试卷审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizExamService.selectBizExamById(id));
    }

    /**
     * 新增试卷审核
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:add')")
    @Log(title = "试卷审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizExam bizExam)
    {
        return toAjax(bizExamService.insertBizExam(bizExam));
    }

    /**
     * 修改试卷审核
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:edit')")
    @Log(title = "试卷审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizExam bizExam)
    {
        return toAjax(bizExamService.updateBizExam(bizExam));
    }

    /**
     * 删除试卷审核
     */
    @PreAuthorize("@ss.hasPermi('exam:exam:remove')")
    @Log(title = "试卷审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizExamService.deleteBizExamByIds(ids));
    }

    /**
     * 提交试卷审核申请
     */
    @Log(title = "试卷审核", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply/{id}")
    @ResponseBody
    public AjaxResult submitApply(@PathVariable Long id) {
        try{
            BizExam bizExam = bizExamService.selectBizExamById(id);
            processService.submitApply(bizExam, "examProcess");
            bizExamService.updateBizExam(bizExam);
        }catch (Exception e) {
            e.printStackTrace();
            return error("提交申请出错：" + e.getMessage());
        }
        return success();
    }
}
